package com.ruoyi.web.controller.materials;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.materials.domain.LwMaterial;
import com.ruoyi.sfproduct.domain.vo.MaterialVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.materials.domain.LwOrder;
import com.ruoyi.materials.service.ILwOrderService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 原材料订单Controller
 * 
 * @author sl
 * @date 2023-12-10
 */
@RestController
@RequestMapping("/materials/order")
public class LwOrderController extends BaseController
{
    @Autowired
    private ILwOrderService lwOrderService;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询原材料订单列表
     */
    @PreAuthorize("@ss.hasPermi('materials:order:list')")
    @GetMapping("/list")
    public TableDataInfo list(LwOrder lwOrder)
    {
        startPage();
        List<LwOrder> list = lwOrderService.selectLwOrderList(lwOrder);
        return getDataTable(list);
    }

    /**
     * 导出原材料订单列表
     */
    @PreAuthorize("@ss.hasPermi('materials:order:export')")
    @Log(title = "原材料订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LwOrder lwOrder)
    {
        List<LwOrder> list = lwOrderService.selectLwOrderList(lwOrder);
        ExcelUtil<LwOrder> util = new ExcelUtil<LwOrder>(LwOrder.class);
        util.exportExcel(response, list, "原材料订单数据");
    }
    /**
     * 导入订单数据
     */
    @Log(title = "订单管理", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<LwOrder> util = new ExcelUtil<>(LwOrder.class);
        List<LwOrder> lwOrderVoList = util.importExcel(file.getInputStream());
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        String operName = loginUser.getUsername();
        String message = lwOrderService.importData(lwOrderVoList, updateSupport, operName);
        return AjaxResult.success(message);
    }
    @PostMapping("/importTemplate")
    public AjaxResult importTemplate()
    {
        ExcelUtil<LwOrder> util = new ExcelUtil<LwOrder>(LwOrder.class);
        return util.importTemplateExcel("订单数据");
    }

    /**
     * 获取原材料订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('materials:order:query')")
    @GetMapping(value = "/{orderId}")
    public AjaxResult getInfo(@PathVariable("orderId") Long orderId)
    {
        return success(lwOrderService.selectLwOrderByOrderId(orderId));
    }

    /**
     * 新增原材料订单
     */
    @PreAuthorize("@ss.hasPermi('materials:order:add')")
    @Log(title = "原材料订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LwOrder lwOrder)
    {
        return toAjax(lwOrderService.insertLwOrder(lwOrder));
    }

    /**
     * 修改原材料订单
     */
    @PreAuthorize("@ss.hasPermi('materials:order:edit')")
    @Log(title = "原材料订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LwOrder lwOrder)
    {
        return toAjax(lwOrderService.updateLwOrder(lwOrder));
    }

    /**
     * 订单下单交付
     */
    @PutMapping("/updateOrderQuantity")
    public AjaxResult updateOrderQuantity(@RequestBody LwOrder lwOrder)
    {
        int result = lwOrderService.updateOrderQuantity(lwOrder);
        if (result == -1) {
            return AjaxResult.error("参数有误");
        } else {
            return toAjax(result);
        }
    }

    /**
     * 订单到货转交
     */
    @PutMapping("/handOverTOActualArrival")
    public AjaxResult handOverTOActualArrival(@RequestBody LwOrder lwOrder)
    {
        int result = lwOrderService.handOverTOActualArrival(lwOrder);
        if (result == -1) {
            return AjaxResult.error("参数有误");
        } else {
            return toAjax(result);
        }
    }

    /**
     * 删除原材料订单
     */
    @PreAuthorize("@ss.hasPermi('materials:order:remove')")
    @Log(title = "原材料订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{orderIds}")
    public AjaxResult remove(@PathVariable Long[] orderIds)
    {
        return toAjax(lwOrderService.deleteLwOrderByOrderIds(orderIds));
    }
}
